//
//  FRQHomeViewController.m
//  FRQBluetoothKit_Example
//
//  Created by admin on 2023/10/22.
//  Copyright © 2023 xuchunhai. All rights reserved.
//

#import "FRQHomeViewController.h"
#import "HomeViewTableViewCell.h"
#import "FRQDevicePairViewController.h"
#import "FRQResellerGalleryViewController.h"
#import "FRQNoImagesYetViewController.h"
#import "FRQNoImagesYetAddNFTViewController.h"
@interface FRQHomeViewController ()
@property (nonatomic,strong) NSArray *arrImages;
@property (nonatomic,strong) NSArray *arrTitle;
@property (nonatomic,strong) NSArray *arrVcName;
@property (nonatomic,strong) NSArray *arrImageList;
@end

@implementation FRQHomeViewController
///app/user/info
//获取账号信息
-(void)appUserInfo{
    
    NSDictionary *dic=@{@"pageNum":@1,
                        @"pageSize":@12
    };
    [skNetTools skGETWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/user/info"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"数据=%@",responseObject);
        skNetModel *model=[skNetModel mj_objectWithKeyValues:responseObject];
        if(model.code==200){
            [UserInfoModel mj_objectWithKeyValues:model.data];
        }
    }];
}
//获取账号信息
-(void)appDeviceMine{
    
    NSDictionary *dic=@{};
    [skNetTools skGETWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/device/mine"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"数据=%@",responseObject);
        skNetModel *model=[skNetModel mj_objectWithKeyValues:responseObject];
        if(model.code==200){
            [MineDeviceInfoModel mj_objectWithKeyValues:model.data];
            [self.tableView reloadData];
        }
    }];
}
-(void)userNftList{
    
    NSDictionary *dic=@{@"pageNum":@1,
                        @"pageSize":@12
    };
    NSString *urlName;
    if(KuserType==0){
        urlName=@"app/user/nft/list";
    }else{
        urlName=@"app/sys/user/nft/list";
    }
    [skNetTools skGETWithURL:[NSString stringWithFormat:@"%@%@",skURL,urlName] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"数据=%@",responseObject);
        skNetModel *model=[skNetModel mj_objectWithKeyValues:responseObject];
        if(model.code==200){
            self.arrImageList=[model.data objectForKey:@"data"];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title=@"Galleries";
    
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    self.tableView.bounds=self.view.bounds;
    self.tableView.backgroundColor=skUIColorFromRGB(0x1c1f2a);
    if(KuserType==0){//用户端
        self.navigationItem.title = @"Galleries";
        self.arrImages=@[@"gallery1_ico_menu",
                         @"gallery2_ico_menu"];
        
        self.arrTitle=@[@"The Manchester Crazy \nGang Gallery",
                        @"My NFT Gallery"];
        self.arrVcName=@[@"FRQResellerGalleryViewController",@"FRQNoImagesYetViewController"];
    }else{//经销商端
        self.navigationItem.title = @"Gallerie";
        self.arrImages=@[@"display_ico_menu",
                         @"add_ico_menu",
                         @"remove_ico_menu"];
        
        self.arrTitle=@[@"Display Gallery",
                        @"Add NFTs",
                        @"Remove NFTs"];
        self.arrVcName=@[@"FRQResellerGalleryViewController",@"FRQAddNFTsViewController",@"FRQResellerGalleryDealerViewController"];
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self appDeviceMine];
    [self userNftList];
    [self appUserInfo];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrTitle.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 78;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 12;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 65;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"HomeViewTableViewCell";
    HomeViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = skXibView(@"HomeViewTableViewCell");
    }
    NSString *imageName=self.arrImages[indexPath.row];
    NSString *titleName=self.arrTitle[indexPath.row];
    cell.imageIcon.image=[UIImage imageNamed:imageName];
    if(KuserType==0){
        if(indexPath.row==0){
            
            NSString *name=[loginModel sharedloginModel].resellerName;
            if(name.length==0){
                name=@"商户";
            }
            cell.labTitle.text=[NSString stringWithFormat:@"%@",name];
        }else{
            cell.labTitle.text=titleName;
        }
    }else{
        cell.labTitle.text=titleName;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *vc=self.arrVcName[indexPath.row];
    if(KuserType==0){//用户端的点击事件
        if(indexPath.row==1){
            if(self.arrImageList.count>0){
                FRQResellerGalleryViewController *view=[[FRQResellerGalleryViewController alloc] init];
                [self.navigationController pushViewController:view animated:YES];
            }else{
                UIViewController *view=[[NSClassFromString(vc) alloc] init];
                [self.navigationController pushViewController:view animated:YES];
            }
        }else{
            FRQResellerGalleryViewController *view=[[FRQResellerGalleryViewController alloc] init];
            view.userShowDealerNft=1;
            [self.navigationController pushViewController:view animated:YES];
        }
    }else{//这里是商户点击事件
        if(indexPath.row==1){
            if(self.arrImageList.count<=0){
                FRQNoImagesYetAddNFTViewController *view=[[FRQNoImagesYetAddNFTViewController alloc] init];
                [self.navigationController pushViewController:view animated:YES];
            }else{
                UIViewController *view=[[NSClassFromString(vc) alloc] init];
                [self.navigationController pushViewController:view animated:YES];
            }
        }else{
            UIViewController *view=[[NSClassFromString(vc) alloc] init];
            [self.navigationController pushViewController:view animated:YES];
        }
    }
}
@end
